package Controller.Menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Utils.MediaUploder;
import Utils.Som.Music;
import View.Frame.PrincipalFrame;
import View.Menu.MenuControlsPanel;
import View.Menu.MenuPanel;
import View.Menu.OptionMenu;
import View.Menu.SMenuPanel;

public class MenuController {
	private MenuPanel menuPanel_;
	private PrincipalFrame principalFrame_;
	private SMenuPanel sMenuPanel_;
	private OptionMenu optionMenu_;
	private MenuOptionController menuOptionController_;
	private MenuControlsPanel menuControlsPanel_;
	private SMenuController sMenuController_;
	private MediaUploder media_;

	public MenuController(PrincipalFrame principalFame, MediaUploder media){
		media_ = media;
		principalFrame_ = principalFame;
		menuPanel_ = new MenuPanel(principalFrame_, this);
		sMenuPanel_ = new SMenuPanel(this);
		optionMenu_ = new OptionMenu();
		menuControlsPanel_ = new MenuControlsPanel();

		principalFrame_.addContentPane(menuPanel_);
		principalFrame_.setMenuController(this);
		menuPanel_.addStartButtonActionListener(new StartButtonActionListener());
		menuPanel_.addOptionButtonActionListener(new OptionActionListener());
		menuPanel_.addQuitButtonActionListener(new QuitActionListener());
		menuPanel_.addControlsActionListener(new ControlActionListener());
		menuControlsPanel_.addBackActionListener(new BackActionListener());
		media.music_.play();
	}

	private class StartButtonActionListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			sMenuController_ = new SMenuController(principalFrame_, sMenuPanel_, media_);
			principalFrame_.getContentPane().removeAll();
			principalFrame_.addContentPane(sMenuPanel_);
			principalFrame_.repaint();
			principalFrame_.setVisible(true);
		}
	}

	private class OptionActionListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			menuOptionController_ = new MenuOptionController(optionMenu_, menuPanel_, principalFrame_);
			principalFrame_.getContentPane().removeAll();
			principalFrame_.addContentPane(optionMenu_);
			principalFrame_.repaint();
			principalFrame_.setVisible(true);
		}
	}
	private class ControlActionListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			principalFrame_.getContentPane().removeAll();
			principalFrame_.addContentPane(menuControlsPanel_);
			principalFrame_.repaint();
			principalFrame_.setVisible(true);
		}
	}
	private class BackActionListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			principalFrame_.getContentPane().removeAll();
			principalFrame_.addContentPane(menuPanel_);
			principalFrame_.repaint();
			principalFrame_.setVisible(true);
		}
	}

	private class QuitActionListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			haltMusic();
			System.exit(0);
		}
	}

	public void haltMusic(){
		media_.music_.halt();
	}

	public MediaUploder getMedia_() {
		return media_;
	}
}
